package datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/9/14.
 */
//创建数据库连接池,只创建一次
public class C3P0Utils {
    //C3P0的数据源对象
    private static ComboPooledDataSource cpds = null;
    private static DataSources ds = null;
    //static 代码块只加载一次
    static{
        cpds=new ComboPooledDataSource();
        //C3P0的数据源对象
        try {
            cpds.setDriverClass("com.mysql.jdbc.Driver");
            cpds.setJdbcUrl("jdbc:mysql://localhost:3306/usertest");
            cpds.setUser("root");
            cpds.setPassword("root");

            cpds.setInitialPoolSize(5);
            cpds.setMaxPoolSize(15);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }

    }
    public static Connection getConnection() throws SQLException {
        return cpds.getConnection();
    }
}
